package com.molyfun.parents.fragment.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.view.common.MfEditText;

/**
 * Created by Administrator on 2017/11/11 0011.
 */

public class MsgSignUpDialogFragment extends Dialog {

    public MsgSignUpDialogFragment(Context context) {
        super(context);
    }

    public MsgSignUpDialogFragment(Context context, int theme) {
        super(context, theme);
    }


    public static class Builder {
        private String message;
        public MfEditText titleView;
        private View contentView;
        private String positiveButtonText;
        private String negativeButtonText;
        private String singleButtonText;
        private View.OnClickListener positiveButtonClickListener;
        private View.OnClickListener negativeButtonClickListener;
        private View.OnClickListener singleButtonClickListener;

        private View layout;
        private MsgSignUpDialogFragment dialog;

        public Builder(Context context) {
            //这里传入自定义的style，直接影响此Dialog的显示效果。style具体实现见style.xml
            dialog = new MsgSignUpDialogFragment(context, R.style.msg_sign_up_dialog_style);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.dialog_msg_sign_up, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            titleView = (MfEditText)layout.findViewById(R.id.content_text);

            layout.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }
        public Builder setPositiveButton(String positiveButtonText, View.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, View.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setSingleButton(String singleButtonText, View.OnClickListener listener) {
            this.singleButtonText = singleButtonText;
            this.singleButtonClickListener = listener;
            return this;
        }

        public String getContentext() {
            String content = ((MfEditText) layout.findViewById(R.id.content_text)).getText().toString();
            return content;
        }


        /**
         * 创建单按钮对话框
         *
         * @return
         */
        public MsgSignUpDialogFragment createSingleButtonDialog() {
            layout.findViewById(R.id.submit_button).setOnClickListener(singleButtonClickListener);
            //如果传入的按钮文字为空，则使用默认的“返回”
            if (singleButtonText != null) {
                ((TextView) layout.findViewById(R.id.submit_button)).setText(singleButtonText);
            }
            create();
            return dialog;
        }


        /**
         * 单按钮对话框和双按钮对话框的公共部分在这里设置
         */
        private void create() {
            if (message != null) {      //设置提示内容
                ((MfEditText) layout.findViewById(R.id.content_text)).setText(message);
                ((MfEditText) layout.findViewById(R.id.content_text)).setFocusable(false);
            }
            /*else if (contentView != null) {       //如果使用Builder的setContentview()方法传入了布局，则使用传入的布局
                ((LinearLayout) layout.findViewById(R.id.content_text)).removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content_text))
                        .addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
            dialog.setContentView(layout);*/
            dialog.setCancelable(true);     //用户可以点击手机Back键取消对话框显示
            dialog.setCanceledOnTouchOutside(false);        //用户不能通过点击对话框之外的地方取消对话框显示
        }

        /**
         * 显示双按钮布局，隐藏单按钮
         * 增加单个button进行修改布局
         */
        private void showTwoButton() {
//            layout.findViewById(R.id.singleButtonLayout).setVisibility(View.GONE);
//            layout.findViewById(R.id.twoButtonLayout).setVisibility(View.VISIBLE);
        }
        public Builder showtitleText(String text) {
            TextView viewById = (TextView) layout.findViewById(R.id.title);
            viewById.setText(text);
            return this;
        }

        public Builder hitCheckIcon(){
             layout.findViewById(R.id.check_ic).setVisibility(View.GONE);
             layout.findViewById(R.id.textView).setVisibility(View.GONE);
            return this;
        }
        public ImageView getIconView() {
            ImageView iconView = (ImageView) layout.findViewById(R.id.check_ic);
            return iconView;
        }
        public TextView getSubmitView() {
            TextView submitView = (TextView) layout.findViewById(R.id.submit_button);
            return submitView;
        }
        /**
         * 显示单按钮布局，隐藏双按钮
         */
        private void showSingleButton() {
//            layout.findViewById(R.id.singleButtonLayout).setVisibility(View.VISIBLE);
//            layout.findViewById(R.id.twoButtonLayout).setVisibility(View.GONE);
        }
        public interface MsgSignUpDialogFragmentCallback{
            void getContentView(String s);
        }
        private MsgSignUpDialogFragmentCallback msgSignUpDialogFragmentCallback;
        public void setMsgSignUpDialogFragmentListener(MsgSignUpDialogFragmentCallback msgSignUpDialogFragmentCallback) {
            this.msgSignUpDialogFragmentCallback =  msgSignUpDialogFragmentCallback;
        }
    }


}